#include<windows.h>
#include<string.h>
#include"resource.h"
#include"bercea.h"
#include"draw.h"


const char g_szClassName[] = "bercaru";

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

	switch(msg)
	{

		case WM_CREATE:
		{
			// DRAW

			CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "DRAW", "Fonts Fundamentals",
				   WS_OVERLAPPEDWINDOW | WS_VISIBLE,CW_USEDEFAULT, CW_USEDEFAULT, 
				   1000, 500, NULL, NULL, GetModuleHandle(NULL) , NULL);
 
			// EDIT BOX

			font = CreateFont(18, 6, 0, 0,
                   FW_NORMAL, FALSE, FALSE, FALSE,
                   ANSI_CHARSET, OUT_DEFAULT_PRECIS,
           	       CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
				   DEFAULT_PITCH | FF_ROMAN,
				   "Times New Roman");
			HWND hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Introduceti cuvantul aici",
					WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
					725, 460, 225, 25, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL),NULL);
			if(hEdit == NULL)
				MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);

			SendMessage(hEdit, WM_SETFONT, (WPARAM)font, MAKELPARAM(FALSE, 0));

			// INSERT
	     
			HWND button = CreateWindow( 
			"BUTTON",                                     // predefined class
			"INSERT",                                         // button text
			WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,     // styles 
		    850,                                          // starting x position
		    500,                                          // starting y position 
		    100,                                           // button width 
		    25,                                           // button height 
		    hwnd,                                         // parent window 
		    (HMENU)IDC_MAIN_BUTTON1,                      // ID 
		    GetModuleHandle(NULL), NULL );

			if(button == NULL)
				MessageBox(hwnd, "Could not create button.", "Error", MB_OK | MB_ICONERROR);
			
			// SEARCH

			HWND button2 = CreateWindow( "BUTTON", "SEARCH", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,     
										725, 500, 100 , 25, hwnd , (HMENU)IDC_MAIN_BUTTON2 , 
										GetModuleHandle(NULL), NULL );

			if(button2 == NULL)
				MessageBox(hwnd, "Could not create button.", "Error", MB_OK | MB_ICONERROR);

			// RESET

			HWND button3 = CreateWindow( "BUTTON", "RESET", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,     
										525, 500, 100 , 25, hwnd , (HMENU)IDC_MAIN_BUTTON3 , 
										GetModuleHandle(NULL), NULL );

			if(button3 == NULL)
				MessageBox(hwnd, "Could not create button.", "Error", MB_OK | MB_ICONERROR);

			//  HELP

			HWND button4 = CreateWindow( "BUTTON", "HELP", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,     
										525, 460, 100 , 25, hwnd , (HMENU)IDC_MAIN_BUTTON4 , 
										GetModuleHandle(NULL), NULL );

			if(button4 == NULL)
				MessageBox(hwnd, "Could not create button.", "Error", MB_OK | MB_ICONERROR);

			// LISTBOX

			HWND List;
			List = CreateWindowEx(WS_EX_CLIENTEDGE  ,
				"LISTBOX",NULL, WS_CHILD | WS_VISIBLE, 50, 460, 400 , 80,
				hwnd,(HMENU)IDC_MAIN_LIST, GetModuleHandle(NULL),NULL);

			SendDlgItemMessage(hwnd, IDC_MAIN_LIST, LB_ADDSTRING, 0, (LPARAM)"OUTPUT :\n");


			if(List == NULL)
				MessageBox(hwnd, "Could not create list box.", "Error", MB_OK | MB_ICONERROR);	
		}
		break;

		case WM_COMMAND :
			switch(LOWORD(wParam))
			{
				case IDC_MAIN_EDIT :
					break;

				case IDC_MAIN_BUTTON1 : 
					{
						int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_MAIN_EDIT));
						if(len > 0 && len < 15)
						{
							char* buf=NULL;
							buf = (char*)GlobalAlloc(GPTR, len + 1);
							GetDlgItemText(hwnd, IDC_MAIN_EDIT, buf, len + 1);
							if(root==NULL)
							{
								adaugare(root,buf);
								strcpy(inserare,"A fost introdus cuvantul : ");
								strcat(inserare,buf);
								SendDlgItemMessage(hwnd, IDC_MAIN_LIST, LB_ADDSTRING, 0, (LPARAM)inserare);
								SendMessage(hwnd,WM_PAINT,(WPARAM )hDC,MAKELPARAM(FALSE, 0));
							}
							else
							{
								if(root->string[0]!=buf[0])
									MessageBox(hwnd, "Cuvantul trebuie sa aiba prefixul cel\n putin egal cu prima litera din radacina !", "Avertisment", MB_OK);
								else
								{
									strcpy(caut,buf);
									strcat(caut,"*");
									if(cautare(root,caut)!=NULL)
									{
										strcpy(inserare,"Cuvantul e deja existent introduceti alt cuvant ");
										SendDlgItemMessage(hwnd, IDC_MAIN_LIST, LB_ADDSTRING, 0, (LPARAM)inserare);
										MessageBox(hwnd, "Introduceti alt cuvant !", "Warning", MB_OK);
									}
									else
									{
										adaugare(root,buf);
										strcpy(inserare,"A fost introdus cuvantul : ");
										strcat(inserare,buf);
										SendDlgItemMessage(hwnd, IDC_MAIN_LIST, LB_ADDSTRING, 0, (LPARAM)inserare);
										SendMessage(hwnd,WM_PAINT,(WPARAM )hDC,MAKELPARAM(FALSE, 0));
									}
								}
							}
							GlobalFree((HANDLE)buf);
						}
						else
						{
							if(len == 0)
								MessageBox(hwnd, "Nu ati introdus nimic !", "Avertisment", MB_OK);
							else
								MessageBox(hwnd, "Cuvantul este prea lung !", "Avertisment", MB_OK);
						}
					}
					break;

				case IDC_MAIN_BUTTON2 :
					{
					int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_MAIN_EDIT));
					if(len > 0)
					{
						char* buf2=NULL;
						buf2 = (char*)GlobalAlloc(GPTR, len + 1);
						GetDlgItemText(hwnd, IDC_MAIN_EDIT, buf2, len + 1);						
						strcpy(caut,buf2);
						strcat(caut,"*");
						if(root!=NULL)
						{
							if(cautare(root,caut)!=NULL)
							{
								MessageBox(hwnd, "Cuvantul a fost gasit !", "CAUTARE", MB_OK);
								strcpy(cautarebuf,"A fost gasit cuvantul : ");
								strcat(cautarebuf,buf2);
								SendDlgItemMessage(hwnd, IDC_MAIN_LIST, LB_ADDSTRING, 0, (LPARAM)cautarebuf);
								m=1;
								SendMessage(hwnd,WM_PAINT,(WPARAM )hDC,0);
							}
							else
							{
								strcpy(cautarebuf,"Nu s-a gasit cuvantul : ");
								strcat(cautarebuf,buf2);
								SendDlgItemMessage(hwnd, IDC_MAIN_LIST, LB_ADDSTRING, 0, (LPARAM)cautarebuf);
								MessageBox(hwnd, "Cuvantul nu a fost gasit !", "CAUTARE", MB_OK);	
							}
						}
						else
							MessageBox(hwnd, "Nu exista nici un cuvant introdus !", "CAUTARE", MB_OK);
						GlobalFree((HANDLE)buf2);
					}
					else 
						MessageBox(hwnd, "Nu ati introdus nimic !", "Avertisment", MB_OK);

					}
					break;

				case IDC_MAIN_BUTTON3 :
					free(root);
					root=NULL;
					SendDlgItemMessage(hwnd, IDC_MAIN_LIST, LB_ADDSTRING, 0, (LPARAM)"Arborele a fost resetat !");
					SendMessage(hwnd,WM_PAINT,(WPARAM )hDC,MAKELPARAM(FALSE, 0));
					break;

				case IDC_MAIN_BUTTON4 :
					MessageBox(hwnd, "APASATI :\n\n   HELP       - pentru ajutor\n   RESET      - pentru resetarea arborelui\n   INSERT    - pentru introducere de cuvinte\n   SEARCH   - pentru cautarea unui cuvant\n", "HELP", MB_OK | MB_ICONINFORMATION);
						break;

			}	
			break;

		case WM_PAINT :

			InvalidateRect(hwnd,NULL,FALSE);

			hDC = BeginPaint(hwnd, &Ps);
			hPen = CreatePen(PS_SOLID, 2 , RGB(225,225,0));
			hcaut = CreatePen(PS_SOLID ,2, RGB(225,0,0));
			font = CreateFont(18, 6, 0, 0,
                   FW_NORMAL, FALSE, FALSE, FALSE,
                   ANSI_CHARSET, OUT_DEFAULT_PRECIS,
           	       CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
				   DEFAULT_PITCH | FF_ROMAN,
				   "Times New Roman");

			// resetarea ecranului
			res = CreatePen(PS_SOLID,250,RGB(255,255,255));
			SelectObject(hDC, res);
			Rectangle(hDC,125,125,875,375);
			resetare();
			

			SelectObject(hDC, font);

			if(m==1)
				colorare_cautare(root,caut,0,500,50);
			if(m==0 && root!=NULL)
				afisare(root,0,500,50);
			m=0;
			DeleteObject(font);
			DeleteObject(hPen);
			DeleteObject(hcaut);
			DeleteObject(res);
			EndPaint(hwnd, &Ps);
			break;

		case WM_CLOSE:
			DestroyWindow(hwnd);
			free_node(root);
			break;

		case WM_DESTROY:
			PostQuitMessage(0);
			free_node(root);
			break;

		default:
			return DefWindowProc(hwnd, msg, wParam, lParam);
	}		
	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc;
	HWND hwnd;
	MSG Msg;
	
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = 0;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;	
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = g_szClassName;
	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);


	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Window Registration Failed!", "Error!",MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,g_szClassName,"BERCARU SOFTWARES @ RADIX TREE DEMONSTRATOR",
							WS_EX_DLGMODALFRAME | WS_SYSMENU, 200, 50, 1000 , 600,
							NULL, NULL, hInstance, NULL);
	if(hwnd == NULL)
	{
		MessageBox(NULL, "Window Creation Failed!", "Error!",
		MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}
 
    if( !hwnd )
        return 0; 

    ShowWindow(hwnd, SW_SHOWNORMAL);
    UpdateWindow(hwnd);

	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return Msg.wParam;
}
